package com.wen.miniowen.controller;

import com.wen.miniowen.config.MinioConfig;
import com.wen.miniowen.util.MinioUtil;
import com.wen.miniowen.util.R;
import io.minio.messages.Bucket;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "文件相关接口")
@Slf4j
@RestController(value = "product/file")
//@RequestMapping(value = "product/file")
public class FileController {

    @Autowired
    private MinioUtil minioUtil;

    @Autowired
    private MinioConfig prop;

    @ApiOperation(value = "查看存储bucket是否存在")
    @ApiImplicitParam(name = "bucketName", value = "存储桶名称", required = true, dataType = "String", paramType = "query")
    @GetMapping("/bucketExists")
    public R bucketExists(@RequestParam("bucketName") String bucketName) {
        return R.ok().put("bucketName", minioUtil.bucketExists(bucketName));
    }

    @ApiOperation(value = "创建存储bucket")
    @ApiImplicitParam(name = "bucketName", value = "存储桶名称", required = true, dataType = "String", paramType = "query")
    @GetMapping("/makeBucket")
    public R makeBucket(@RequestParam("bucketName") String bucketName) {
        return R.ok().put("bucketName", minioUtil.makeBucket(bucketName));
    }

    @ApiOperation(value = "删除存储bucket")
    @ApiImplicitParam(name = "bucketName", value = "存储桶名称", required = true, dataType = "String", paramType = "query")
    @GetMapping("/removeBucket")
    public R removeBucket(@RequestParam("bucketName") String bucketName) {
        return R.ok().put("bucketName", minioUtil.removeBucket(bucketName));
    }

    @ApiOperation(value = "获取全部bucket")
    @GetMapping("/getAllBuckets")
    public R getAllBuckets() {
        List<Bucket> allBuckets = minioUtil.getAllBuckets();
        Bucket bucket = allBuckets.get(0);
        log.info("bucketName:" + bucket.name() + ",creationDate:" + bucket.creationDate());
        return R.ok().put("allBuckets", allBuckets);
    }

    @ApiOperation(value = "文件上传返回url")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "__file", paramType = "form")
    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file) {
        String objectName = minioUtil.upload(file);
        if (null != objectName) {
            return R.ok().put("url", (prop.getEndpoint() + "/" + prop.getBucketName() + "/" + objectName));
        }
        return R.error(" ");
    }

    @ApiOperation(value = "图片/视频预览")
    @ApiImplicitParam(name = "fileName", value = "文件名", required = true, dataType = "String", paramType = "query")
    @GetMapping("/preview")
    public R preview(@RequestParam("fileName") String fileName) {
        return R.ok().put("fileName", minioUtil.preview(fileName));
    }

    @ApiOperation(value = "文件下载")
    @ApiImplicitParam(name = "fileName", value = "文件名", required = true, dataType = "String", paramType = "query")
    @GetMapping("/download")
    public R download(@RequestParam("fileName") String fileName, HttpServletResponse res) {
        minioUtil.download(fileName, res);
        return R.ok();
    }

    @ApiOperation(value = "删除文件", notes = "根据url地址删除文件")
    @ApiImplicitParam(name = "url", value = "文件URL", required = true, dataType = "String", paramType = "query")
    @PostMapping("/delete")
    public R remove(@RequestParam("url") String url) {
        String objName = url.substring(url.lastIndexOf(prop.getBucketName() + "/") + prop.getBucketName().length() + 1);
        minioUtil.remove(objName);
        return R.ok().put("objName", objName);
    }
}
